<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/3.0.2/vue.global.js"></script>
</head>
<body>
    <div id="app"></div>
</body>
<script>
    const app = Vue.createApp({
        data(){
            return{
                showItem:'zujianone'
            }
        },
        methods:{
            clickqiehuan(){
                this.showItem = this.showItem == 'zujianone'?'zujiantwo':'zujianone';
            }
        },
        // 动态组件优化代码
        template:`
        <div>
            <button @click="clickqiehuan">切换</button>
            <keep-alive>
                <component :is="showItem"/>    
            </keep-alive>
        </div>
        `
    })
    app.component('zujianone',{
        // 动态组件中的状态保存
        template:`
        <input />
        <img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201608%2F16%2F20160816114536_NuvtF.png&refer=http%3A%2F%2Fb-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1631340980&t=45d623cbce56a26f73e5280510dd194c"/>`
    })
    app.component('zujiantwo',{
        template:'<img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwww.znjchina.com%2Fd%2Ffile%2F2018-05-04%2F1525426381806974.jpg&refer=http%3A%2F%2Fwww.znjchina.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1631341043&t=fafb3b692f32626c35e87414392458fa"/>'
    })
    app.mount("#app")
</script>
</html>